<%--
  Created by IntelliJ IDEA.
  Customer: XBB
  Date: 2021/2/18
  Time: 14:04
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui_ext/dtree/dtree.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui_ext/dtree/font/dtreefont.css">
</head>
<body>
<div style="padding: 10px">

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>查询条件</legend>
    </fieldset>
    <form  class="layui-form">
        <div class="layui-form-item">
            <div class="layui-form-item">
                <label class="layui-form-label">身份证号</label>
                <div class="layui-input-inline">
                    <input type="text" name="identity"   placeholder="请输入身份证号" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label">客户姓名</label>
                <div class="layui-input-inline">
                    <input type="text" name="customerName"   placeholder="请输入客户姓名" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">地址</label>
                <div class="layui-input-inline">
                    <input type="text" name="address"   placeholder="请输入地址" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label">电话</label>
                <div class="layui-input-inline">
                    <input type="text" name="phone"   placeholder="请输入电话" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label">性别</label>
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="radio" name="sex" value="1" title="男">
                        <input type="radio" name="sex" value="0" title="女">
                    </div>
                </div>
            </div>
            <div class="layui-form-item" style="text-align: right">
                <button  class="layui-btn layui-btn-normal layui-icon layui-icon-search layui-btn-radius layui-btn-sm" lay-submit lay-filter="doSearch">查询
                </button>
                <button type="reset" class="layui-btn layui-btn-warm layui-icon layui-icon-refresh layui-btn-radius layui-btn-sm">重置
                </button>
            </div>
        </div>
    </form>

    <table id="customerTable" lay-filter="customerTableFilter"></table>
</div>

<script id="toolbarcustomer" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="customerAdd">添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="deleteSome">批量删除</button>
    </div>
</script>

<script id="barcustomer" type="text/html">
    <a class="layui-btn layui-btn-xs layui-btn-radius" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs layui-btn-radius" lay-event="del">删除</a>
</script>

<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 20px" id="saveOrUpdateDiv">
    <form class="layui-form" lay-filter="dataFrm" id="dataFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">身份证号码:</label>
            <div class="layui-input-block">
                <input id="identityInput" type="text" name="identity" placeholder="请输入身份证号码" lay-verify="required" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">姓名:</label>
            <div class="layui-input-block">
                <input type="text" name="customerName" placeholder="请输入客户姓名" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">性别:</label>
                <div class="layui-input-inline">
                    <input type="radio" name="sex" value="1" checked="checked" title="男">
                    <input type="radio" name="sex" value="0" title="女">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">地址:</label>
            <div class="layui-input-block">
                <input type="text" name="address" placeholder="请输入客户地址" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">电话:</label>
            <div class="layui-input-block">
                <input type="text" name="phone" placeholder="请输入客户电话" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">职位:</label>
            <div class="layui-input-block">
                <input type="text" name="career" placeholder="请输入职位" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;padding-right: 120px">
                <button type="button" class="layui-btn layui-btn-normal layui-btn-md layui-icon layui-icon-release layui-btn-radius" lay-filter="doSubmit" lay-submit="">&nbsp; 提交</button>
                <button type="reset" class="layui-btn layui-btn-warm layui-btn-md layui-icon layui-icon-refresh layui-btn-radius">&nbsp; 重置</button>
            </div>
        </div>
    </form>
</div>


<script type="text/javascript" src="${pageContext.request.contextPath}/static/layui/layui.js"></script>
<script>

    var customerTab;
    var customerIndex;
    var title;

    layui.use(['form','layer','jquery','table'],function() {
        var form = layui.form, layer = layui.layer, $ = layui.jquery,table = layui.table;

        customerTab = table.render({
            elem:"#customerTable"
            ,url:"${pageContext.request.contextPath}/business/customer/customerRightJson"
            ,cols:[[
                {type:'checkbox'}
                ,{field:'identity',title:'客户身份证号',align:"center",sort:true}
                ,{field:'customerName',title:'客户姓名',align:"center"}
                ,{field:'sex',title:'性别',align:"center",templet:function (r) {
                        return r.sex == 1 ? '男':'女'
                    }}
                ,{field:'address',title:'地址',align:"center"}
                ,{field:'phone',title:'电话',align:"center"}
                ,{field:'career',title:'职位',align:"center"}
                ,{field:'createTime',title:'创建时间',align:"center"}
                ,{fixed:'right',title:'操作',align:"center",toolbar:'#barcustomer',minWidth:200}
            ]]
            ,toolbar:'#toolbarcustomer'
            ,page:true
            ,limit:[5,10,15]
            ,limit:5
        })

        //给查询按钮添加事件
        form.on('submit(doSearch)',function (data) {

            customerTab.reload({
                where:data.field
                ,page:{curr:1}
            })

            return false;
        })

        //监听头部工具栏
        table.on('toolbar(customerTableFilter)',function (obj) {
            if(obj.event == "customerAdd"){
                customerAdd()
            }else if(obj.event == "deleteSome"){
                //获取选中的行
                var checkStatus = table.checkStatus(obj.config.id)
                if(checkStatus.data.length == 0){
                    layer.msg("至少选择一行数据")
                    return false
                }
                //获取所有选中的customerId
                var str = ""
                for(var i = 0;i<checkStatus.data.length;i++){
                    var customer = checkStatus.data[i];
                    str += customer.identity + ","
                }
                deleteSome(str,checkStatus.data.length)
            }
        })

        function deleteSome(str,count) {
            layer.confirm("您确定要删除这"+count+"条数据吗？",{icon:2,title:'批量删除'},function(i){
                $.post("${pageContext.request.contextPath}/business/customer/deleteSomeCustomer",{"customerIds":str},function(r){
                    if(r.code == 0){
                        //删除成功
                        layer.msg(r.msg, {
                            offset: '15px'
                            ,icon: 1
                            ,time: 1000
                        }, function(){
                            // 刷新table
                            customerTab.reload({
                                page:{curr:1}
                            })
                        });
                    }else{
                        //删除失败
                        layer.msg(r.msg)
                    }
                })
            })
        }

        //点击添加按钮的弹出
        function customerAdd() {
            customerIndex = layer.open({
                type:1
                ,title:"添加客户"
                ,area:['700px','700px']
                ,content:$('#saveOrUpdateDiv')
            })
            //清除表单的内容
            $("#dataFrm").get(0).reset()
            $("#identity").val("")
            form.render()

            title = "添加"

            $("#carNumberInput").removeClass("layui-disabled")
        }

        //给弹出层的form表单submit按钮添加点击事件
        form.on('submit(doSubmit)',function (obj) {
            layer.confirm("您确定要"+title+"吗？",function () {
                $.post("${pageContext.request.contextPath}/business/customer/customerAddOrUpdate",obj.field,function (r) {
                    if(r.code == 0){
                        layer.msg(r.msg,{
                            offset:'15px'
                            ,icon:1
                            ,time:1000
                        },function () {
                            //关闭当前弹出框
                            layer.close(customerIndex)
                            //刷新table
                            customerTab.reload({
                                page:{curr:1}
                            })
                        });
                    }else{
                        layer.msg(r.msg)
                    }

                })
            })
            return false;
        })

        //监听行工具栏
        table.on('tool(customerTableFilter)',function (obj) {
            if(obj.event == "edit"){
                edit(obj.data)
            }else if(obj.event == "del"){
                del(obj.data)
            }
        })

        //点击编辑
        function edit(data) {
            customerIndex = layer.open({
                type:1
                ,title:"修改客户"
                ,area:['700px','500px']
                ,content:$('#saveOrUpdateDiv')
            })

            form.val('dataFrm',data)
            form.render()
            //给表单设置值

            $("#identityInput").attr("disabled",true).addClass("layui-disabled")
            title = "修改"
        }

        //点击删除
        function del(data){
            layer.confirm("确定删除"+data.customerName+"吗？",function (i) {
                $.post("${pageContext.request.contextPath}/business/customer/customerDelete",{"id":data.identity},function (r) {
                    if(r.code == 0){
                        layer.msg(r.msg,{
                            offset:'15px'
                            ,icon:1
                            ,time:1000
                        },function () {
                            //关闭当前弹出框
                            layer.close(customerIndex)
                            //刷新table
                            customerTab.reload({
                                page:{curr:1}
                            })
                        });
                    }else{
                        layer.msg(r.msg)
                    }

                })
            })
        }

    })
</script>
</body>
</html>

